#include "XmlHelper.h"
#include <sstream>
#include <chrono>
#include <iomanip>

// 从XML中提取指定标签的值
std::string XmlHelper::extractValue(const std::string& xml, const std::string& tag) {
    std::regex pattern("<" + tag + ">(.*?)</" + tag + ">");
    std::smatch match;
    if (std::regex_search(xml, match, pattern) && match.size() > 1) {
        return match[1].str();
    }
    return "";
}

// 获取当前日期时间
std::string XmlHelper::getCurrentTime() {
    auto now = std::chrono::system_clock::now();
    auto now_c = std::chrono::system_clock::to_time_t(now);
    char time_buf[64];
    
    #ifdef _WIN32
    struct tm timeinfo;
    localtime_s(&timeinfo, &now_c);
    strftime(time_buf, sizeof(time_buf), "%Y-%m-%dT%H:%M:%S", &timeinfo);
    #else
    strftime(time_buf, sizeof(time_buf), "%Y-%m-%dT%H:%M:%S", localtime(&now_c));
    #endif
    
    return std::string(time_buf);
}

// 获取当前日期
std::string XmlHelper::getCurrentDate() {
    auto now = std::chrono::system_clock::now();
    auto now_c = std::chrono::system_clock::to_time_t(now);
    char time_buf[64];
    
    #ifdef _WIN32
    struct tm timeinfo;
    localtime_s(&timeinfo, &now_c);
    strftime(time_buf, sizeof(time_buf), "%Y-%m-%d", &timeinfo);
    #else
    strftime(time_buf, sizeof(time_buf), "%Y-%m-%d", localtime(&now_c));
    #endif
    
    return std::string(time_buf);
}

// 生成Catalog响应内容
std::string XmlHelper::generateCatalogResponse(const std::string& sn) {
    Config& config = Config::getInstance();
    
    std::stringstream ss;
    ss << "<?xml version=\"1.0\" encoding=\"GB2312\"?>" << std::endl
       << "<Response>" << std::endl
       << "  <CmdType>Catalog</CmdType>" << std::endl
       << "  <SN>" << sn << "</SN>" << std::endl
       << "  <DeviceID>" << config.getDeviceId() << "</DeviceID>" << std::endl
       << "  <SumNum>1</SumNum>" << std::endl
       << "  <DeviceList Num=\"1\">" << std::endl
       << "    <Item>" << std::endl
       << "      <DeviceID>" << config.getChannelId() << "</DeviceID>" << std::endl
       << "      <Name>V5摄像头通道1</Name>" << std::endl
       << "      <Manufacturer>" << config.getManufacturer() << "</Manufacturer>" << std::endl
       << "      <Model>" << config.getModel() << "</Model>" << std::endl
       << "      <Owner>V5</Owner>" << std::endl
       << "      <CivilCode>" << config.getRealm() << "</CivilCode>" << std::endl
       << "      <Address>本地测试地址</Address>" << std::endl
       << "      <Parental>0</Parental>" << std::endl
       << "      <ParentID>" << config.getDeviceId() << "</ParentID>" << std::endl
       << "      <SafetyWay>0</SafetyWay>" << std::endl
       << "      <RegisterWay>1</RegisterWay>" << std::endl
       << "      <Secrecy>0</Secrecy>" << std::endl
       << "      <Status>ON</Status>" << std::endl
       << "    </Item>" << std::endl
       << "  </DeviceList>" << std::endl
       << "</Response>";
    return ss.str();
}

// 生成设备状态响应内容
std::string XmlHelper::generateDeviceStatusResponse(const std::string& sn) {
    Config& config = Config::getInstance();
    
    std::stringstream ss;
    ss << "<?xml version=\"1.0\" encoding=\"GB2312\"?>" << std::endl
       << "<Response>" << std::endl
       << "  <CmdType>DeviceStatus</CmdType>" << std::endl
       << "  <SN>" << sn << "</SN>" << std::endl
       << "  <DeviceID>" << config.getDeviceId() << "</DeviceID>" << std::endl
       << "  <Result>OK</Result>" << std::endl
       << "  <Online>ONLINE</Online>" << std::endl
       << "  <Status>OK</Status>" << std::endl
       << "  <DeviceTime>" << getCurrentTime() << "</DeviceTime>" << std::endl
       << "  <Alarmstatus>0</Alarmstatus>" << std::endl
       << "</Response>";
    return ss.str();
}

// 生成设备信息响应内容
std::string XmlHelper::generateDeviceInfoResponse(const std::string& sn) {
    Config& config = Config::getInstance();
    
    std::stringstream ss;
    ss << "<?xml version=\"1.0\" encoding=\"GB2312\"?>" << std::endl
       << "<Response>" << std::endl
       << "  <CmdType>DeviceInfo</CmdType>" << std::endl
       << "  <SN>" << sn << "</SN>" << std::endl
       << "  <DeviceID>" << config.getDeviceId() << "</DeviceID>" << std::endl
       << "  <Result>OK</Result>" << std::endl
       << "  <DeviceName>" << config.getDeviceName() << "</DeviceName>" << std::endl
       << "  <Manufacturer>" << config.getManufacturer() << "</Manufacturer>" << std::endl
       << "  <Model>" << config.getModel() << "</Model>" << std::endl
       << "  <Firmware>" << config.getFirmware() << "</Firmware>" << std::endl
       << "  <Channel>1</Channel>" << std::endl
       << "  <SIMNum></SIMNum>" << std::endl
       << "</Response>";
    return ss.str();
}

// 生成PTZ控制响应内容
std::string XmlHelper::generatePTZResponse(const std::string& sn, bool success) {
    Config& config = Config::getInstance();
    
    std::stringstream ss;
    ss << "<?xml version=\"1.0\" encoding=\"GB2312\"?>" << std::endl
       << "<Response>" << std::endl
       << "  <CmdType>PTZCmd</CmdType>" << std::endl
       << "  <SN>" << sn << "</SN>" << std::endl
       << "  <DeviceID>" << config.getChannelId() << "</DeviceID>" << std::endl
       << "  <Result>" << (success ? "OK" : "ERROR") << "</Result>" << std::endl
       << "</Response>";
    return ss.str();
}

// 生成录像文件列表响应
std::string XmlHelper::generateRecordInfoResponse(const std::string& sn) {
    Config& config = Config::getInstance();
    
    // 生成一个示例录像列表，实际应用中应从存储中读取
    std::string currentDate = getCurrentDate();
    std::string startTime = currentDate + "T00:00:00";
    std::string endTime = currentDate + "T00:10:00";
    
    std::stringstream ss;
    ss << "<?xml version=\"1.0\" encoding=\"GB2312\"?>" << std::endl
       << "<Response>" << std::endl
       << "  <CmdType>RecordInfo</CmdType>" << std::endl
       << "  <SN>" << sn << "</SN>" << std::endl
       << "  <DeviceID>" << config.getChannelId() << "</DeviceID>" << std::endl
       << "  <SumNum>1</SumNum>" << std::endl
       << "  <RecordList Num=\"1\">" << std::endl
       << "    <Item>" << std::endl
       << "      <DeviceID>" << config.getChannelId() << "</DeviceID>" << std::endl
       << "      <Name>录像1</Name>" << std::endl
       << "      <StartTime>" << startTime << "</StartTime>" << std::endl
       << "      <EndTime>" << endTime << "</EndTime>" << std::endl
       << "      <Secrecy>0</Secrecy>" << std::endl
       << "      <Type>time</Type>" << std::endl
       << "      <FileSize>10240</FileSize>" << std::endl
       << "    </Item>" << std::endl
       << "  </RecordList>" << std::endl
       << "</Response>";
    return ss.str();
} 